import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class Lector {
	
	private String globalPath="";
	
	public Tarjeta Leer() throws FileNotFoundException, IOException, ClassNotFoundException{
		Tarjeta temp = new Tarjeta();
		String path="";
		JFileChooser JFileChooser1 = new  JFileChooser("C:/");
		FileFilter Filter = new SimpleFileFilter("card");
		JFileChooser1.setFileFilter(Filter);
		/////////////////////////////////////////
		int val = JFileChooser1.showOpenDialog(null);
		if (val == JFileChooser.APPROVE_OPTION)
		{
			path = JFileChooser1.getSelectedFile().getPath();
			globalPath=path;
			System.out.println(path);
			
			ObjectInputStream leetarj = new ObjectInputStream(new FileInputStream(path));
			try
			{
				temp = (Tarjeta)leetarj.readObject();
			}catch(EOFException eof){temp=null;}
		}	
		/////////////////////////////////////////		
		return temp;
	}	
	
	public String getGlobalPath(){
		return globalPath;
	}
	
	public class SimpleFileFilter extends FileFilter {

		String[] extensions;
		String description;

		public SimpleFileFilter(String ext) {
			this (new String[] {ext}, null);
		}

		public SimpleFileFilter(String[] exts, String descr) {
			extensions = new String[exts.length];
			for (int i = exts.length - 1; i >= 0; i--) {
				extensions[i] = exts[i].toLowerCase();
			}
			description = (descr == null ? exts[0] + " files" : descr);
		}

		public boolean accept(File f) {
			if (f.isDirectory()) { return true; }

			String name = f.getName().toLowerCase();
			for (int i = extensions.length - 1; i >= 0; i--) {
				if (name.endsWith(extensions[i])) {
					return true;
				}
			}
			return false;
		}

		public String getDescription() { return description; }
	}
	
}
